//
//  MCShareTicketViewController.swift
//  StoreManage
//
//  Created by MC on 2018/1/31.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCShareTicketViewController: MCViewController {

    var model : MCTicketModel = MCTicketModel() {
        didSet {
            ticketMoneyLabel.text = MCString_judgemantDecimalsIsZero(figure: model.amount.intValue)
            ticketNameLabel.text = model.promotionDetailName
            
            var moneyOrDiscountStr : String
            
            if (model.isDiscount == 0) {
                let moneyStr = MCString_judgemantDecimalsIsZero(figure: model.amount.intValue)
                

                moneyOrDiscountStr = moneyStr.MCString_suffix(index: 1) + "元"
                
                ticketLimitLabel.text = "满" + MCString_judgemantDecimalsIsZero(figure: model.limited.intValue).MCString_suffix(index: 1) + "元" + "使用"
            } else {
                let discount : CGFloat = CGFloat((100 - self.model.discount.floatValue) / 10.0)
                moneyOrDiscountStr = String.init(format: "%.1f折", discount)
                ticketLimitLabel.text = "满\(model.limited.intValue/100)元使用"
            }
            
            ticketMoneyLabel.attributedText = moneyOrDiscountStr.MCAttributedString_font(font: 18, range: NSRange.init(location: moneyOrDiscountStr.count - 1, length: 1))
            
            switch model.type.intValue {
            case 2:    // 通用券
                ticketTypeImageView.image = #imageLiteral(resourceName: "Ticket_normal")
            case 3:    // 品牌券
                ticketTypeImageView.image = #imageLiteral(resourceName: "Ticket_brand")
            case 5:    // 商品券
                ticketTypeImageView.image = #imageLiteral(resourceName: "Ticket_goods")
            default:
                break
            }
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func rightItemClicked() {
        view.window?.addSubview(shareWayView)
        shareWayView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view.window!)
        }
    }
    
    // MARK: - Network
    func sendNetworking() {
        let paras = [
            "promotionDetailId" : model.promotionDetailId
        ]
        
        MCNetwork_post(url: JAVA_getShareSmallProgramsCode, para: paras, vc: self, isHiddenTabbar: true, success: { (response) in
            
            let dataDict = response["data"] as? [String:Any] ?? [:]
            let urlStr = dataDict["imgUrl"] as? String ?? ""
            
            self.qrImageView.sd_setImage(with: URL.init(string: urlStr), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    // MARK: - Method
    func baseSetting() {
        self.title = "发送优惠券"
        self.view.backgroundColor = UIColor.white
        
        
        navigationItem.rightBarButtonItem = MCSetBarButtonItem_text("分享", target: self, selector: #selector(rightItemClicked))
    }
    
    func initUI() {


        
        view.addSubview(bgView)
        bgView.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(25)
            make.width.equalTo(MCScreenWidth - 50)
            make.top.equalTo(50)
            make.height.equalTo((MCScreenWidth - 50) * 1.34)
        }
        
//        bgView.addSubview(bgImageView)
//        bgImageView.snp.remakeConstraints { (make) ->Void in
//            make.edges.equalTo(bgView)
//        }
//
//        return
        
        bgView.addSubview(topImageView)
        topImageView.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(40)
            make.left.equalTo(22)
            make.right.equalTo(-22)
            make.height.equalTo(topImageView.snp.width).multipliedBy(0.39)
        }
        
        bgView.addSubview(storeNameLabel)
        storeNameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(15)
            make.top.equalTo(topImageView.snp.bottom).offset(25)
        }
        
        topImageView.addSubview(ticketTypeImageView)
        ticketTypeImageView.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-15)
            make.top.equalTo(topImageView)
        }
       
        
        topImageView.addSubview(ticketLimitLabel)
        ticketLimitLabel.snp.remakeConstraints { (make) -> Void in
            make.bottom.equalTo(topImageView.snp.centerY).offset(-5)
            make.left.equalTo(topImageView.snp.centerX).offset(-2)
        }

        
        topImageView.addSubview(ticketMoneyLabel)
        ticketMoneyLabel.snp.remakeConstraints { (make) -> Void in
            make.right.equalTo(ticketLimitLabel.snp.left).offset(-28)
            make.bottom.equalTo(ticketLimitLabel).offset(5)
        }



        topImageView.addSubview(ticketNameLabel)
        ticketNameLabel.snp.remakeConstraints { (make) -> Void in
            make.left.equalTo(topImageView).offset(15)
            make.right.equalTo(topImageView).offset(-15)
            make.top.equalTo(ticketLimitLabel.snp.bottom).offset(15)
        }


        bgView.addSubview(qrImageView)
        qrImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.width.height.equalTo(127)
            make.top.equalTo(storeNameLabel.snp.bottom).offset(30)
        }
        

        
        bgView.addSubview(promptLabel)
        promptLabel.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView)
            make.top.equalTo(qrImageView.snp.bottom).offset(15)
        }
    }
    
    
    // MARK: - Setter & Getter
//    lazy var bgView: MCShareBaseBackgroundView = {
//        let view = MCShareBaseBackgroundView()
//        view.backgroundColor = UIColor.clear
////        view.cropView.backgroundColor = UIColor.MCMain
//        return view
//    }()
    
    lazy var bgView: UIImageView = {
        let iv = UIImageView()
        iv.image = #imageLiteral(resourceName: "Ticket_bg")
        return iv
    }()
    
    lazy var shareWayView: MCShareBaseShareWayView = {
        let view = MCShareBaseShareWayView()
        view.superView = self.view
        view.saveView = self.bgView
        return view
    }()
    
    lazy var topImageView: UIImageView = {
        let iv = UIImageView()
        iv.image = #imageLiteral(resourceName: "Ticket_brand_head")
        return iv
    }()
    
    lazy var ticketTypeImageView: UIImageView = {
        let iv = UIImageView()
        return iv
    }()


    
    lazy var storeNameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        label.text = MCUserinfo_getStoreName()
        label.textColor = UIColor.white
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    private lazy var qrImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 5
        imageView.layer.masksToBounds = true
        imageView.backgroundColor = UIColor.white
        return imageView
    }()
    
    private lazy var ticketMoneyLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(35)
        label.textColor = UIColor.MCBlue
        return label
    }()
    
    
    private lazy var ticketNameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        label.textColor = UIColor.MCBlue
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    private lazy var ticketLimitLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        label.textColor = UIColor.MCBlue
        return label
    }()
    
    private lazy var promptLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.white
        label.textAlignment = NSTextAlignment.center
        label.text =  "扫描识别二维码进入小程序\n领取优惠券"
        return label
    }()
    
}
